package com.smrs.asn.service;

import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.jof.framework.dao.hibernate.BaseDAOHibernateImpl;
import com.jof.framework.util.Pager;
import com.smrs.asn.dao.AsnDetailDao;
import com.smrs.asn.dao.AsnHeaderDao;
import com.smrs.asn.model.AsnDetailModel;
import com.smrs.asn.model.AsnHeaderModel;
import com.smrs.asn.model.AsnHeaderSearchModel;
import com.smrs.service.BaseService;

@Component(value="asnHeaderService")
public class AsnHeaderService extends BaseService<AsnHeaderModel,Long>{
	@Autowired
	private AsnHeaderDao asnHeaderDao;
	
	@Autowired
	private AsnDetailDao asnDetailDao;


	public Pager<AsnHeaderModel> getByNameLikePager(String name, Pager<AsnHeaderModel> pager){
		return asnHeaderDao.getByNameLikePager(name,pager);
		
	}

	public Pager<AsnHeaderModel> findAsnHeader(AsnHeaderSearchModel asnHeaderSearch, Pager<AsnHeaderModel> pager){
		
		Pager<AsnHeaderModel> tempPager = asnHeaderDao.findAsnHeader(asnHeaderSearch, pager);
		return tempPager;
	}
	
	public void updateCalculateAsnHeaderQuantifyAndAmount(AsnHeaderModel asnHeader){
		List<AsnDetailModel> list=asnDetailDao.findByAsnHeader(asnHeader);
		if(CollectionUtils.isNotEmpty(list)){
			AsnHeaderModel headerModel = this.getByPK(asnHeader.getId());
			Double totalQuantify =0d;
			Double totalAmount = 0d;
			for(AsnDetailModel oneDetail : list){
				if(oneDetail.getActualQuantify()!=null){
					totalQuantify = totalQuantify+oneDetail.getActualQuantify();
				}
				if(oneDetail.getActualAmount()!=null){
					totalAmount = totalAmount +oneDetail.getActualAmount();
				}
			}
			headerModel.setActualQuantify(totalQuantify);
			headerModel.setActualAmount(totalAmount);
			this.updateModel(headerModel);
		}
	}
	
	
	@Override
	public BaseDAOHibernateImpl<AsnHeaderModel, Long> getPerformDao() {		
		return asnHeaderDao;
	}

	
	
}
